<template>
	<view class="">
		<template>
			<!-- 订单待支付金额 -->
			<view class="text-center p-y-25 text-13">
				<!-- 	<view class="flex align-center justify-center m-b-6">
					<text class="m-r-5 color-33">支付剩余时间</text>
					<tui-countdown :time="expiredTime" @end="getPayTypeList" />
				</view> -->
				<view class="flex align-center justify-center m-b-6">
					<basic-price :decimal="36" :number="36" :price="payInfo?.payAmount || 0" :unit="20" :weight="500"
						color="#000" />
				</view>
				<view class="flex align-center justify-center color-99">
					<text class="m-r-6">订单编号：</text>
					<text>{{ payInfo?.payCode || '111' }}</text>
				</view>
			</view>
		</template>
		<view class="flex-1 flex align-center justify-center p-x-12">
			<tui-button :disabled="payLoading" :loading="payLoading" block class="flex-1 common-tab-btn text-14"
				shape="circle" type="primary" @click="platformPay">
				确认交易
			</tui-button>
		</view>
		<!-- </basic-tabbar> -->
	</view>
</template>

<script setup name="cashier">
	import {
		reactive,
		ref
	} from 'vue'
	import {
		AppPay
	} from '@/util/platform/app/pay'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		debounce
	} from 'lodash'
	import {
		bjPrepay
	} from '@/api/index'
	import uniApi from '@/util/uniApi/index'
	import {
		getQueryValueByKey
	} from '@/util/lib/common'

	import $store from '@/store/index'
	// import payPopUp from '@/pages-subpackage/common/components/diyPayPopup'
	// import byCodePopUp from '../../components/codePopup'

	// 跳转过来支付的参数信息
	const payParams = ref({
		payCode: ''
	})

	// 页面支付信息
	const payInfo = ref(null)

	const expiredTime = ref(0)

	// 支付方式['新华阅读卡支付', '微信支付等']
	const payTypeObj = reactive({
		normal: []
	})
	// 阅读卡支付弹窗及必要卡信息
	const payPopup = ref(false)
	const payCardInfo = reactive({
		cardNo: '',
		cardPwd: ''
	})
	// 支付loading
	const payLoading = ref(false)

	const showByPayCode = ref(false)
	const byCode = ref('')

	onLoad((op) => {
		payInfo.value = op

	})

	// 平台级支付
	const platformPay = debounce(async () => {
		// 拉起支付
		bjPrepay({
				orderSn: payInfo.value.orderSn
			}, 'post')
			.then((res) => {
				if (res.errno == 0) {
					let payStr = {}
					const payType = "WX_APP"
					res.data.package = res.data.packageValue
					payStr = res.data
					// 异常参数处理
					if (!payStr) {
						uni.showToast({
							title: '拉起支付参数错误',
							icon: 'none'
						})
						return reject('拉起支付参数错误')
					}
					// 获取服务提供商

					// 支付参数
					let payParams = {}
					if (payType === 'WX_APP') {
						payParams.provider = 'wxpay'
						payParams = payStr
					}
					console.log(payParams);
					// 发起支付
					uni.requestPayment({
						...payParams,
						success: ({
							errMsg
						}) => {
							console.log(errMsg, 'errMsg');
							if (errMsg == 'requestPayment:ok') {
								uni.showToast({
									title: '支付成功',
									icon: 'none'
								})
								uni.switchTab({
									url: '/pages/orderList/orderList'
								})
							}
						},
						fail: (err) => {
							console.log('fail:', err)
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
							// uni.switchTab({
							// 	url: '/pages/orderList/orderList'
							// })
						}
					})
				} else {
					uni.showToast({
						title: '失败',
						icon: 'none'
					})
				}

			})
	}, 500)

	// 可公用的初始支付参数
	function getPayInitialValue() {
		const params = {
			payCode: payParams.value.payCode,
			payWay: ''
		}
		params.payWay = payWayInfo.payWay
		params.payChannel = payWayInfo.channel

		if (payWayInfo?.cardInfo) {
			params.cardNo = payWayInfo.cardInfo.cardNo
		}

		return params
	}
	// 查询支付方式
	async function getPayTypeList() {
		let data = {
			payAmount: payParams.value.payAmount,
			payCode: payParams.value.payCode
		}
		payInfo.value = data
		countdown({
				orderId: payInfo.value.payCode
			}, 'get')
			.then((res) => {
				if (res.code == 200) {
					expiredTime.value = res.msg
					if (res.msg == "0") {
						uni.switchTab({
							url: "/pages/orderList/orderList"
						})
					}
				}
			})
	}

	// 切换支付方式
	function changePayType(index, type) {
		const ApiFunc = (selectList, otherList) => {
			payTypeObj[selectList].forEach((item, i) => {
				item.isSelect = index === i
				if (item.cards) {
					item.cards = item.cards.map(v => {
						v.isSelect = false
						return v
					})
				}
			})
			payTypeObj[otherList].forEach(item => {
				item.isSelect = false
				if (item.cards) {
					item.cards = item.cards.map(v => {
						v.isSelect = false
						return v
					})
				}
			})
			payCardInfo.cardNo = ''
			payCardInfo.cardPwd = ''
		}
		const otherType = 'normal'
		ApiFunc(type, otherType)
	}

	// 退出支付页面
	function cancelPayBack() {
		uniApi.modal({
			content: '确认离开收银台？',
			cancelText: '继续支付',
			confirmText: '确认离开',
			callback: () => {
				$store('app').clearPayCashierParams()
				switch (payParams.value.jumpType) {
					case 0:
						const pageUrl = payParams.value.backUrlKey
						uni.switchTab({
							url: pageUrl
						})
						// uniApi.redirect(`${pageUrl}?${payParams.value.orderParams}`)
						break
					case 1:
						uniApi.pageBack()
						break
				}
			}
		})
	}

	// 判断同一支付单是否有未支付的支付单
	async function testIsSamePayOrder() {
		const params = getPayInitialValue()
		let isSamePayOrder = ['WX_APP', 'WX_LITE'].includes(params.payWay)
		if (Boolean(Number(getQueryValueByKey('payTips', payParams.value.orderParams))) && !isSamePayOrder) {
			const {
				data
			} = await QueryPaymentCode({
				payCode: payParams.value.payCode,
				payWay: params.payWay
			})
			if (data) {
				uniApi.comfirmModal({
					content: data
				})
				payLoading.value = false
				return false
			}
		}
		return true
	}
</script>

<style lang="scss" scoped>
	.pay-Box {
		&-border {
			border-bottom: rpx2(0.5) solid #fafafa;
		}
	}

	.account-wrap-item {
		background-color: #F7F7F7;
		border: rpx2(1) solid #F7F7F7;

		&.isSelect {
			border: rpx2(1) solid #F2450B;
			background: #FFEDEC;
		}

		&.isUsed {
			opacity: .6;
			color: #ccc !important;
		}
	}
</style>